package com.fanshilz.flower2025.apps.admin.sysDictItem.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import com.fanshilz.flower2025.apps.admin.sysDictItem.req.SysDictItemSaveOrUpdateReq;
import com.fanshilz.flower2025.apps.admin.sysDictItem.req.SysDictItemTableReq;
import com.fanshilz.flower2025.apps.admin.sysDictItem.resp.SysDictItemTableDetailResp;
import com.fanshilz.flower2025.apps.admin.sysRole.resp.SelectResp;
import com.fanshilz.flower2025.apps.openapi.req.DictTypeListReq;
import com.fanshilz.flower2025.business.sys.sysDictItem.entity.SysDictItem;
import com.fanshilz.flower2025.business.sys.sysDictItem.service.ISysDictItemService;
import com.fanshilz.flower2025.framework.exception.CodeException;
import com.fanshilz.flower2025.framework.resp.Resp;
import com.fanshilz.flower2025.framework.resp.TableResp;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.github.linpeilie.Converter;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.fanshilz.flower2025.business.sys.sysDictItem.entity.table.SysDictItemTableDef.SYS_DICT_ITEM;

@RequestMapping("/admin/dictItem")
@RestController
public class SysDictItemController {

    @Resource
    private Converter converter;
    @Resource
    private ISysDictItemService sysDictItemService;

    @PostMapping("/dictTypeList")
    public Resp<List<SelectResp>> dictTypeList(@RequestBody @Valid DictTypeListReq req) {
        List<SysDictItem> list = sysDictItemService.list(QueryWrapper.create()
                .where(SYS_DICT_ITEM.DICT_KEY.eq(req.getKey())));
        List<SelectResp> resp = list.stream().map(di -> new SelectResp(di.getDictItemLabel(), Long.valueOf(di.getDictItemValue()))).toList();
        return Resp.success(resp);
    }

    @PostMapping("/dictTypeMapList")
    public Resp<Map<String, List<SelectResp>>> dictTypeMapList(@RequestBody @Valid DictTypeListReq req) {
        List<SysDictItem> list = sysDictItemService.list(QueryWrapper.create()
                .where(SYS_DICT_ITEM.DICT_KEY.in(req.getKeys())));

        Map<String, List<SysDictItem>> group = list.stream().collect(Collectors.groupingBy(SysDictItem::getDictKey));

        Map<String, List<SelectResp>> resp = MapUtil.newHashMap();
        group.forEach((k, v) -> resp.put(k, v.stream().map(di -> new SelectResp(di.getDictItemLabel(), Long.valueOf(di.getDictItemValue()))).toList()));

        return Resp.success(resp);
    }

    @PostMapping("/remove/{dictItemId}")
    public Resp<Void> remove(@PathVariable Long dictItemId) {
        boolean b = sysDictItemService.updateById(SysDictItem.builder()
                .dictItemId(dictItemId)
                .delFlag(true)
                .build());
        Assert.isTrue(b, CodeException::new);
        return Resp.success();
    }

    @PostMapping("/saveOrUpdate")
    public Resp<Void> saveOrUpdate(@RequestBody @Valid SysDictItemSaveOrUpdateReq req) {
        SysDictItem di = converter.convert(req, SysDictItem.class);
        boolean b = sysDictItemService.saveOrUpdate(di);
        Assert.isTrue(b, CodeException::new);
        return Resp.success();
    }

    @PostMapping("/table")
    public Resp<TableResp<SysDictItemTableDetailResp>> table(@RequestBody @Valid SysDictItemTableReq req) {
        Page<SysDictItemTableDetailResp> page = sysDictItemService.pageAs(Page.of(req.getCurrent(), req.getPageSize(), req.getTotal()),
                QueryWrapper.create(), SysDictItemTableDetailResp.class);
        return Resp.success(new TableResp<>(page.getRecords(), page.getTotalRow()));
    }

}
